<?php
class GraderModule extends Module {
	public function index() {
		global $conf;
		requireAdmin();
		$this->set('file', $conf['grader-time']);
		$this->render('grader-status');
	}
	
	public function start() {
		global $conf;
		requireAdmin();

		$this->set('file', $conf['grader-time']);
		
		ob_start();
		
		$old_cwd = getcwd();
		$dir = BASE_PATH . '/scripts/';
		chdir($dir);
		
		$command = 'sudo ./evaluator-start.sh';
		shell_exec($command);
		
		chdir($old_cwd);

		ob_end_clean();

		redirect(WEB_ROOT . '/grader?start=1');
	}
	
	public function stop() {
		global $conf;
		requireAdmin();

		$this->set('file', $conf['grader-time']);

		$old_cwd = getcwd();
		$dir = BASE_PATH . '/scripts/';
		chdir($dir);
		
		$command = 'sudo ./evaluator-stop.sh';
		system($command);
		
		chdir($old_cwd);

		redirect(WEB_ROOT . '/grader?stop=1');
	}
	
	public function status() {
		requireAdmin();
		$this->render('grader');
	}
}
